﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading;


namespace CopyIt
{
    public partial class DisplayCopier : UserControl
    {
        public event EventHandler Close;
        public event EventHandler Report;
        public delegate void UpdateFormDelegate(CopierEventArgs args);
        UpdateFormDelegate UpdateDisplay;
        Copier copier = null;
        DateTime StartCopy;
        List<PairsParameters> Params = null;

        public DisplayCopier(TreeView tree, List<PairsParameters> Params)
        {
            InitializeComponent();
            this.Params = Params;

            copier = new Copier(tree.Nodes);
            copier.FilesCopied += new Copier.CopierHandler(copier_FilesCopied);
            copier.BeginSyncronize += new EventHandler(copier_BeginSyncronize);
            copier.EndSyncronize += new EventHandler(copier_EndSyncronize);
            copier.UpdateBackup += new Copier.UpdateBackupHandler(copier_UpdateBackup);

            UpdateDisplay = new UpdateFormDelegate(UpdateForm);
        }

        void copier_UpdateBackup(object sender, UpdateBackupArgs e)
        {
            foreach (PairsParameters pairs in Params)
                if (pairs.Guid == e.Guid)
                    pairs.LastBackup = DateTime.Now;
        }

        void copier_EndSyncronize(object sender, EventArgs e)
        {
            tspbTotal.Visible = false;
            tspbPartial.Visible = false;
            tssLabel.Visible = false;
            tsbReport.Enabled = true;
            tsbExit.Enabled = true;
            tsbStop.Visible = false;
            this.Cursor = Cursors.Default;
            toolStrip1.Cursor = Cursors.Default;
        }

        void copier_BeginSyncronize(object sender, EventArgs e)
        {
            tspbTotal.Visible = true;
            tspbPartial.Visible = true;
            tssLabel.Visible = true;
            StartCopy = DateTime.Now;
            tsbReport.Enabled = false;
            tsbExit.Enabled = false;
            tsbStop.Visible = true;
            this.Cursor = Cursors.WaitCursor;
            toolStrip1.Cursor = Cursors.Default;
        }

        void copier_FilesCopied(object sender, CopierEventArgs e)
        {
            try
            {
                if (!lblByteCopied.InvokeRequired)
                {
                    UpdateDisplay(e);
                }
                else
                {
                    lblFileToCopy.BeginInvoke(UpdateDisplay, new object[] { e });
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Write(ex.Message);
            }
        }

        private void UpdateForm(CopierEventArgs e)
        {
            tssLabel.Text = e.Filename;
            tspbPartial.Value = (int)(e.PercCurFile);
            tspbTotal.Value = (int)e.PercFiles;
            lblTrasferRate.Text = e.CurTrasfRate != 0 ? e.CurTrasfRate.ToString("##,##0.00") : "";
            lblFileToCopy.Text = e.FileToCopy.ToString("###,###0");
            lblFileCopied.Text = e.FileCopied.ToString("###,###0");
            lblByteToCopy.Text = e.ByteToCopy.ToString("###,###,###,###");
            lblByteCopied.Text = e.ByteCopied.ToString("###,###,###,###");

            if (e.ByteCopied == 0)
                return;

            TimeSpan ElapsedTime = (DateTime.Now - StartCopy);

            // Calcolo quanti byte al tick copio
            double byteTick = (double)ElapsedTime.Ticks / (double)e.ByteCopied;

            //Moltiplico i byte da copiare per i tickrate
            long resByte = (long)(e.ByteToCopy-e.ByteCopied);
            TimeSpan resTime = new TimeSpan((long)((double)byteTick * (double)resByte));
            lblResTime.Text = new DateTime(resTime.Ticks).ToLongTimeString();

            //// Moltiplico i byte totali per il tick rate per tempo totale stimato
            TimeSpan totTime = new TimeSpan((long)((double)byteTick * (double)e.ByteToCopy));
            lblEstTime.Text = new DateTime(totTime.Ticks).ToLongTimeString();

            //double byteRate = (double)totByteCopied / (double)diffTime.Ticks*10000;
            //lblTrasfRate.Text = byteRate.ToString("###,###,###,###.###");

            if (e.Errors > 0)
                lblFileError.Text = e.Errors.ToString("###,###");

        }

        public void Start(out string report)
        {
            copier.Start(out report);
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            if (Close != null)
                Close(this, e);
        }

        private void tsbReport_Click(object sender, EventArgs e)
        {
            if (Report != null)
                Report(this, e);
        }

        private void tsbStop_Click(object sender, EventArgs e)
        {
            copier.StopCopy();
        }
    }
}
